package gui;
import java.awt.Color;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.MutableTreeNode;

import model.Mp3Data;
import model.NodeMp3;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import control.FileIO;


/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE. 
 */
public class MainFrame extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3961359892710699275L;
	private JTree navigationTree;
	private JPanel coverPanel;
	private JTextField yearTextField;
	private JLabel titleLabel;
	private JLabel yearLabel;
	private JLabel albumLabel;
	private JLabel aristLabel;
	private JTextField artistTextField;
	private JTextField albumTextField;
	private JTextField titleTextField;
	
	//private static final Color BACKGROUNDCOLOR = new Color(200,200,200);
	private static final Color COVERBACKGROUNDCOLOR = new Color(200,200,200);

	private ImageIcon icon;
	private JLabel fileNameLabel;
	private JLabel label;

	private NodeMp3 previousNode = new NodeMp3(new Mp3Data("1", "2", "3", "4", "5"));

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MainFrame inst = new MainFrame();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public MainFrame() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {

			AnchorLayout thisLayout = new AnchorLayout();
			getContentPane().setLayout(thisLayout);
			{
				fileNameLabel = new JLabel();
				getContentPane().add(fileNameLabel, new AnchorConstraint(26, 979, 59, 616, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				fileNameLabel.setPreferredSize(new java.awt.Dimension(290, 16));
			}
			{
				coverPanel = new JPanel();
				
				getContentPane().add(coverPanel, new AnchorConstraint(432, 980, 969, 527, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				coverPanel.setPreferredSize(new java.awt.Dimension(272, 203));
				coverPanel.setBackground(COVERBACKGROUNDCOLOR);
			}
			{
				navigationTree = new JTree(control.FileIO.buildTree());

			}
			{
				titleTextField = new JTextField();
			}
			{
				yearTextField = new JTextField();
				getContentPane().add(yearTextField, new AnchorConstraint(342, 695, 400, 610, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				yearTextField.setPreferredSize(new java.awt.Dimension(51, 22));
			}
			{
				albumTextField = new JTextField();
			}
			{
				artistTextField = new JTextField();
			}
			{
				aristLabel = new JLabel();
				aristLabel.setText("Artist");
			}
			{
				titleLabel = new JLabel();
				titleLabel.setText("Title");
			}
			{
				albumLabel = new JLabel();
				albumLabel.setText("Album");
			}
			{
				yearLabel = new JLabel();
				getContentPane().add(yearLabel, new AnchorConstraint(350, 590, 390, 519, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				getContentPane().add(albumTextField, new AnchorConstraint(252, 980, 310, 610, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				getContentPane().add(albumLabel, new AnchorConstraint(260, 590, 300, 519, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				getContentPane().add(titleTextField, new AnchorConstraint(162, 980, 220, 610, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				getContentPane().add(titleLabel, new AnchorConstraint(170, 590, 210, 519, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				getContentPane().add(artistTextField, new AnchorConstraint(72, 980, 130, 610, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				getContentPane().add(aristLabel, new AnchorConstraint(80, 590, 120, 519, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				getContentPane().add(navigationTree, new AnchorConstraint(33, 507, 969, 20, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				navigationTree.setPreferredSize(new java.awt.Dimension(292, 354));
				aristLabel.setPreferredSize(new java.awt.Dimension(43, 15));
				artistTextField.setPreferredSize(new java.awt.Dimension(222, 22));
				titleLabel.setPreferredSize(new java.awt.Dimension(43, 15));
				titleTextField.setPreferredSize(new java.awt.Dimension(222, 22));
				albumLabel.setPreferredSize(new java.awt.Dimension(43, 15));
				albumTextField.setPreferredSize(new java.awt.Dimension(222, 22));
				yearLabel.setText("Year");
				yearLabel.setPreferredSize(new java.awt.Dimension(43, 15));
			}

			navigationTree.addTreeSelectionListener(new TreeSelectionListener() {

				@Override
				public void valueChanged(TreeSelectionEvent e) {
					MutableTreeNode node = (MutableTreeNode)
					navigationTree.getLastSelectedPathComponent();

					if (node == null) return;
					if (node instanceof NodeMp3){

						
						//swap text field information and store into last selected
						//mp3 node
						previousNode.mp3.setAlbum(albumTextField.getText());
						previousNode.mp3.setTitle(titleTextField.getText());
						previousNode.mp3.setArtist(artistTextField.getText());
						previousNode.mp3.setYear(yearTextField.getText());

						artistTextField.setText(((NodeMp3)node).mp3.getArtist());
						albumTextField.setText(((NodeMp3)node).mp3.getAlbum());
						titleTextField.setText(((NodeMp3)node).mp3.getTitle());
						yearTextField.setText(((NodeMp3)node).mp3.getYear());
						fileNameLabel.setText(((NodeMp3)node).mp3.getFileName());
						
						//get and draw cover image
						BufferedImage buf = FileIO.loadCover((NodeMp3)node);
						
						icon = new ImageIcon(buf);
						label = new JLabel(icon);
						
						coverPanel.removeAll();
						coverPanel.add(label);
						coverPanel.revalidate();
						repaint();
						
						//store current mp3 node as the future "previous" node
						previousNode = (NodeMp3) node;

					}
				}
			});

			pack();
			this.setSize(800, 500);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
